1 package net.sourceforge.simplegamenet.dice; 2 3 import java.io.Serializable; 4 import net.sourceforge.simplegamenet.specs.model.GameServer; 5 import net.sourceforge.simplegamenet.specs.model.ServerEngine; 6 import net.sourceforge.simplegamenet.specs.to.PlayerSettings; 7 8 public class DiceServer extends GameServer implements DicePacketCodes, 9 DiceSettingOptions { 10 11 private DiceScore diceScore; 12 private DiceAll diceAll; 13 private DicePacket dicePacket; 14 private int[] settingValues; 15 private Integer thisPlayer; 16 private Integer nextPlayer; 17 18 private Integer[] diceParticipantIDs; 19 20 public DiceServer(ServerEngine serverEngine) { 21 super(serverEngine); 22 } 23 24 private int nextPlayer(int diceParticipantIndex) { 25 for (int i = 0; i < diceParticipantIDs.length; i++) { 26 if (diceParticipantIDs[i] 27 .equals(diceParticipantIDs[diceParticipantIndex])) { 28 return (i + 1 == diceParticipantIDs.length) ? 0 : (i + 1); 29 } 30 } 31 return 0; 32 } 33 34 public void sendDicePacket(DicePacket dicePacket) { 35 serverEngine.sendData(dicePacket); 36 } 37 38 public void receiveData(Integer diceParticipantID, Serializable data) { 39 DicePacket dicePacket = (DicePacket) data; 40 int[] filledScore = new int[3]; 41 switch (dicePacket.dicePacketCode) { 42 case GAME_NEXT_PLAYER: 43 nextPlayer = 44 new Integer(nextPlayer(((Integer) dicePacket.diceData).intValue())); 45 serverEngine.sendData(new DicePacket(GAME_NEXT_PLAYER, nextPlayer)); 46 break; 47 case TURN_ROLL_DICE: 48 boolean[] selectedDice = (boolean[]) dicePacket.diceData; 49 int[] rolledDice = diceAll.rollDice(selectedDice); 50 serverEngine.sendData(new DicePacket(TURN_DICE_ROLLED, rolledDice)); 51 break; 52 case TURN_HOLD_DICE: 53 diceAll.holdDice(((Integer) dicePacket.diceData).intValue()); 54 serverEngine.sendData(new DicePacket(TURN_DICE_HELD, dicePacket.diceData)); 55 break; 56 case TURN_FILL_SCORE: 57 for (int i = 0; i < diceParticipantIDs.length; i++) { 58 if (diceParticipantID.equals(diceParticipantIDs[i])) { 59 filledScore[0] = i; 60 } 61 } 62 thisPlayer = diceParticipantID; 63 filledScore[1] = ((Integer) dicePacket.diceData).intValue(); 64 filledScore[2] = diceScore.countScore(thisPlayer, filledScore[1]); 65 if (filledScore[2] != 0) { 66 diceScore.setPlayerScore(thisPlayer, filledScore[1], filledScore[2]); 67 serverEngine.sendData(new DicePacket(TURN_SCORE_FILLED, filledScore)); 68 if (filledScore[1] == 10 || filledScore[1] == 11 || filledScore[1] == 12 69 || filledScore[1] == 13) { 70 diceScore.adjustSettingValues(filledScore[1]); 71 } 72 } else { 73 serverEngine.sendData(diceParticipantID, 74 new DicePacket(TURN_SCORE_FILLED, filledScore)); 75 } 76 break; 77 case TURN_CONFIRM_ZERO: 78 filledScore = (int[]) dicePacket.diceData; 79 diceScore.setPlayerScore(thisPlayer, filledScore[1], filledScore[2]); 80 serverEngine.sendData(new DicePacket(TURN_ZERO_CONFIRMED, filledScore)); 81 break; 82 case BONUS_CHECK: 83 int[] filledBonus = (int[]) dicePacket.diceData; 84 if (filledBonus[2] > 0) { 85 diceScore.adjustSettingValues(filledBonus[1]); 86 } 87 if (filledScore[2] == -1) { 88 diceScore.setPlayerScore(thisPlayer, filledBonus[1], 0); 89 } else { 90 diceScore.setPlayerScore(thisPlayer, filledBonus[1], filledBonus[2]); 91 } 92 serverEngine.sendData(new DicePacket(BONUS_CHECKED, filledBonus)); 93 break; 94 case GAME_OVER: 95 gameOver(); 96 break; 97 default : 98 System.out.println("Unrecognized packet"); 99 } 100 } 101 102 public void gameStarted() { 103 diceParticipantIDs = serverEngine.getPlayerSettingsMap().getParticipantIDs(); 104 Integer[] participantsOrder = new Integer[diceParticipantIDs.length]; 105 int diceParticipantIDsLength = diceParticipantIDs.length; 106 for (int i = 0; i < participantsOrder.length; i++) { 107 int randomNumberIndex = (int) (Math.random() * (diceParticipantIDsLength)); 108 participantsOrder[i] = diceParticipantIDs[randomNumberIndex]; 109 for (int j = randomNumberIndex; j < diceParticipantIDsLength - 1; j++) { 110 diceParticipantIDs[j] = diceParticipantIDs[j + 1]; 111 } 112 diceParticipantIDsLength--; 113 } 114 diceParticipantIDs = participantsOrder; 115 diceAll = new DiceAll(); 116 diceScore = new DiceScore(diceParticipantIDs, getDiceSettings(), diceAll); 117 serverEngine.sendData(new DicePacket(GAME_FIRST_SCORE, diceScore)); 118 serverEngine.sendData(new DicePacket(GAME_FIRST_PLAYER, diceParticipantIDs)); 119 } 120 121 public Integer[] getDiceParticipantIDs() { 122 return diceParticipantIDs; 123 } 124 125 public DiceSettings getDiceSettings() { 126 return (DiceSettings) serverEngine.getGameSettings(); 127 } 128 129 public int[] getSettingValues() { 130 return getDiceSettings().getSettingValues(); 131 } 132 133 public void gameAborted() { 134 serverEngine.appendGameChatMessage("Game Over"); 135 } 136 137 public String getDicePlayerNickname(Integer dicePlayerID) { 138 return serverEngine.getPlayerSettingsMap() 139 .getPlayerSettings(dicePlayerID).getNickname(); 140 } 141 142 private void gameOver() { 143 int[] winner = diceScore.getWinner(); 144 if (winner[1] == 0 || !containsPlayerID(getDiceParticipantIDs()[winner[0]])) { 145 serverEngine.appendGameChatMessage("It's a tie game"); 146 } else { 147 serverEngine.appendGameChatMessage("The winner is " + 148 getDicePlayerNickname(getDiceParticipantIDs()[winner[0]]) + " with " + 149 winner[1] + " points!"); 150 } 151 serverEngine.stopGame(); 152 } 153 154 public void playerRemoved(PlayerSettings playerSettings) { 155 if (serverEngine.isGamePlaying() 156 && playerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 157 gameOver(); 158 } 159 } 160 161 public boolean containsPlayerID(Integer playerID) { 162 return serverEngine.getPlayerSettingsMap().containsPlayerID(playerID); 163 } 164 165 }